package managedbean;

import util.enumeration.Country;
import exception.ExistException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.SystemMessagesLocal;
import session.stateless.StaffAccountManagerLocal;
import util.entityhelper.StaffAccountState;
import util.enumeration.AccessRight;
import util.enumeration.Department;
import util.enumeration.Gender;
import util.enumeration.JobPosition;
import util.localization.WebLocalization;

/**
 *
 * @author Wen Yong
 */
@ManagedBean
@RequestScoped
public class StaffRegisterManagedBean implements Serializable {
    @EJB
    private StaffAccountManagerLocal staffAccountMgr;
    // display exception
    @EJB
    private SystemMessagesLocal systemMessages;
    
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private StaffAccountState staffAcctState;
    
    //  user preferred locale
    private String preferredLocale;
    
    // other fields from database
    private String username;
//    private String password;
//    private String confirmPassword;
    private String lastname;
    private String firstname;
    private String gender;
    private Date birthdate;
    private String country;
    private String locale;
    private String email;
    private String emailSecondary;
    private String department;
    private String jobPosition;
    private Date joinDate;
    private List<AccessRight> accessRightsList;
    
//    private String statusPasswordLength;
//    private String statusPasswordMatch;

    public StaffRegisterManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
//        statusPasswordLength = systemMessages.getSystemMessage("TOURIST_ACCT_PASSWORD_LENGTH_LESS_THAN_6");
//        statusPasswordMatch = systemMessages.getSystemMessage("TOURIST_ACCT_PASSWORDS_NOT_MATCH");
    }
    
        public void registerStaff(ActionEvent event) {    
        try {   
            /* validations first */
            if (checkUsernameAvailability(username) == false) {
                // print error message
                String statusMessage = systemMessages.getSystemMessage("USERNAME_NOT_OK", preferredLocale);
                FacesContext.getCurrentInstance().addMessage(username, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
            }
            else {
                // set language for all MANAGERS for all THROWABLE display exceptions in backend
                // to use this line everytime language changes
                staffAccountMgr.setSelectedLocale(preferredLocale);

                // reformat all other required fields
                Gender g = Gender.valueOf(gender);
                Country c = Country.valueOf(country);
                Department d = Department.valueOf(department);
                JobPosition jp = JobPosition.valueOf(jobPosition);
//                // force reformat Date all the time to fit database format
//                DateFormat formatter;
//                formatter = new SimpleDateFormat("MM/dd/yyyy");
//                String formattedBd = formatter.format(birthdate);
//                Date bd = formatter.parse(formattedBd);
//                String formattedJd = formatter.format(joinDate);
//                Date jd = formatter.parse(formattedJd);
                
                // email = username
                // password is auto-generated by backend
                staffAccountMgr.createStaffAccount(username, lastname, firstname, g, birthdate, c, locale, email, emailSecondary, d, jp, joinDate, accessRightsList);

                // print success message
                String statusMessage = systemMessages.getSystemMessage("ACCOUNT_CREATION_SUCCESS", preferredLocale);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
            }
        }
        catch (ExistException ex) {
            // display exception
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
        catch (Exception ex) {
            // display exception tp user
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
            // backend message for Tcube
            ex.printStackTrace();   
        }
    }
        
    
    private boolean validateEmailFormat(String emailInput) {
        Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
        Matcher m = p.matcher(emailInput);
        boolean matchFound = m.matches();

        if (!matchFound) {
            // means error
            return false;
        }
        return true;
    }
    
//    public void checkUsernameAvailability(ActionEvent event) {
    private boolean checkUsernameAvailability(String username) {
        boolean isAvailable = false;
        
        try {
            // set language for all MANAGERS for all THROWABLE display exceptions in backend
            // to use this line everytime language changes
            staffAccountMgr.setSelectedLocale(preferredLocale);
            
            staffAcctState = null;
            try {
                staffAcctState = staffAccountMgr.getStaffAccountByUsername(username);
            }
            catch (Exception ex) {
                // do nothing, just let the system catch any exception and continue to next part for catching of null
            }
            
            if (staffAcctState == null) {
                // print ok message
//                String statusMessage = systemMessages.getSystemMessage("USERNAME_OK", preferredLocale);
//                FacesContext.getCurrentInstance().addMessage(username, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
                isAvailable = true;
            }
            else {
                
            }
        }
//        catch (ExistException ex) {
//            // display exception
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
//        }
        catch (Exception ex) {
            // display exception to user
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
            // backend message for Tcube
            ex.printStackTrace();   
        }
        
        return isAvailable;
    }

    public StaffAccountManagerLocal getStaffAccountMgr() {
        return staffAccountMgr;
    }

    public void setStaffAccountMgr(StaffAccountManagerLocal staffAccountMgr) {
        this.staffAccountMgr = staffAccountMgr;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public SystemMessagesLocal getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesLocal systemMessages) {
        this.systemMessages = systemMessages;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmailSecondary() {
        return emailSecondary;
    }

    public void setEmailSecondary(String emailSecondary) {
        this.emailSecondary = emailSecondary;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }
    
    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getJobPosition() {
        return jobPosition;
    }

    public void setJobPosition(String jobPosition) {
        this.jobPosition = jobPosition;
    }

    public Date getJoinDate() {
        return joinDate;
    }

    public void setJoinDate(Date joinDate) {
        this.joinDate = joinDate;
    }

    public StaffAccountState getStaffAcctState() {
        return staffAcctState;
    }

    public void setStaffAcctState(StaffAccountState staffAcctState) {
        this.staffAcctState = staffAcctState;
    }

    public List<AccessRight> getAccessRightsList() {
        return accessRightsList;
    }

    public void setAccessRightsList(List<AccessRight> accessRightsList) {
        this.accessRightsList = accessRightsList;
    }

    
}
